<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>webbench源码剖析 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/20.b5af7fad.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link router-link-active">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link router-link-active">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>webbench源码剖析</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_01-剖析背景" class="sidebar-link">01.剖析背景</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_02-展示webbench效果" class="sidebar-link">02.展示webbench效果</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_03-图示源码" class="sidebar-link">03.图示源码</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_04-makefile分析" class="sidebar-link">04.makefile分析</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_05-源码注释版" class="sidebar-link">05.源码注释版</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_5-1-socket-c" class="sidebar-link">5.1.socket.c</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_5-2-webbench-c主体函数剖析" class="sidebar-link">5.2.webbench.c主体函数剖析</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_5-3-webbench-c" class="sidebar-link">5.3.webbench.c</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#函数build-request" class="sidebar-link">函数build_request</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#函数bench" class="sidebar-link">函数bench</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#函数benchcore" class="sidebar-link">函数benchcore</a></li></ul></li><li><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_06-提取剖析知识" class="sidebar-link">06.提取剖析知识</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_6-1-代码规范" class="sidebar-link">6.1.代码规范</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_6-2-编程技巧" class="sidebar-link">6.2.编程技巧</a></li><li class="sidebar-sub-header"><a href="/01.Language/C/SourceCode/webbench%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90.html#_6-3-词汇整理" class="sidebar-link">6.3.词汇整理</a></li></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="webbench源码剖析"><a href="#webbench源码剖析" class="header-anchor">#</a> webbench源码剖析</h1> <h2 id="_01-剖析背景"><a href="#_01-剖析背景" class="header-anchor">#</a> 01.剖析背景</h2> <ul><li>✅源码版本：<code>webbench-1.5</code></li> <li>⬇️下载链接：官网<a href="http://home.tiscali.cz/~cz210552/webbench.html" target="_blank" rel="noopener noreferrer">下载链接<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li>👩‍💻 👨‍💻开发者：由Lionbridge公司开发</li> <li>⭐️软件类型：Webbench是一个网站压力测试的工具</li> <li>🔋 使用语言及版本：纯C</li> <li>依赖环境/包：</li> <li>⚡️源码行数『如果容易测试』：注释+代码≈500行</li></ul> <blockquote><p>详解介绍：</p></blockquote> <ul><li><p>Webbech的标准测试可以向我们展示服务器的两项内容，分别为</p> <ul><li>每秒钟请求数</li> <li>每秒钟传输数据量。</li></ul></li> <li><p>如果你不清数访问的网站能承受多大的压力，或者对比两个网站的性能可以，考虑使用Webbench或者http_load来测试。</p></li> <li><p>webbench不但能具有便准静态页面的测试能力，还能对动态页面（ASP,PHP,JAVA,CGI）进行测试的能力。还有就是他支持对含有SSL的安全网站例如电子商务网站进行静态或动态的性能测试。</p></li> <li><p>有人说：<strong>Webbench最多可以模拟3万个并发连接去测试网站的负载能力。</strong></p></li> <li><p>bench，n,<strong>试验台</strong>，工作台，长椅</p></li></ul> <h2 id="_02-展示webbench效果"><a href="#_02-展示webbench效果" class="header-anchor">#</a> 02.展示webbench效果</h2> <ul><li>原理：
<ul><li>1.主函数进行参数命令行检查，并且进入bench开始压测。</li> <li>2.bench函数使用fork模拟出多个客户端，调用socket并发请求，每个子进程记录自己的访问数据，并写入管道。</li> <li>3.父进程从管道读取子进程的输出信息。</li> <li>4.使用函数alarm进行时间控制，到时候后会发生SIGALRM信号，调用信号处理函数子进程停止。</li></ul></li></ul> <h2 id="_03-图示源码"><a href="#_03-图示源码" class="header-anchor">#</a> 03.图示源码</h2> <ul><li>webbench源码一共包含两个源文件：<code>socket.c</code>和<code>webbench.c</code></li></ul> <h2 id="_04-makefile分析"><a href="#_04-makefile分析" class="header-anchor">#</a> 04.makefile分析</h2> <div class="language-makefile line-numbers-mode"><pre class="language-makefile"><code>CFLAGS<span class="token operator">?=</span>	-Wall -ggdb -W -O
CC<span class="token operator">?=</span>		gcc
LIBS<span class="token operator">?=</span>
LDFLAGS<span class="token operator">?=</span>
PREFIX<span class="token operator">?=</span>	/usr/local
VERSION<span class="token operator">=</span>1.5
TMPDIR<span class="token operator">=</span>/tmp/webbench-<span class="token variable">$</span><span class="token punctuation">(</span>VERSION<span class="token punctuation">)</span>

<span class="token symbol">all</span><span class="token punctuation">:</span>   webbench tags

<span class="token symbol">tags</span><span class="token punctuation">:</span>  *.c
	-ctags *.c

<span class="token symbol">install</span><span class="token punctuation">:</span> webbench
	install -s webbench <span class="token variable">$</span><span class="token punctuation">(</span>DESTDIR<span class="token punctuation">)</span><span class="token variable">$</span><span class="token punctuation">(</span>PREFIX<span class="token punctuation">)</span>/bin	
	install -m 644 webbench.1 <span class="token variable">$</span><span class="token punctuation">(</span>DESTDIR<span class="token punctuation">)</span><span class="token variable">$</span><span class="token punctuation">(</span>PREFIX<span class="token punctuation">)</span>/man/man1	
	install -d <span class="token variable">$</span><span class="token punctuation">(</span>DESTDIR<span class="token punctuation">)</span><span class="token variable">$</span><span class="token punctuation">(</span>PREFIX<span class="token punctuation">)</span>/share/doc/webbench
	install -m 644 debian/copyright <span class="token variable">$</span><span class="token punctuation">(</span>DESTDIR<span class="token punctuation">)</span><span class="token variable">$</span><span class="token punctuation">(</span>PREFIX<span class="token punctuation">)</span>/share/doc/webbench
	install -m 644 debian/changelog <span class="token variable">$</span><span class="token punctuation">(</span>DESTDIR<span class="token punctuation">)</span><span class="token variable">$</span><span class="token punctuation">(</span>PREFIX<span class="token punctuation">)</span>/share/doc/webbench

<span class="token symbol">webbench</span><span class="token punctuation">:</span> webbench.o Makefile
	<span class="token variable">$</span><span class="token punctuation">(</span>CC<span class="token punctuation">)</span> <span class="token variable">$</span><span class="token punctuation">(</span>CFLAGS<span class="token punctuation">)</span> <span class="token variable">$</span><span class="token punctuation">(</span>LDFLAGS<span class="token punctuation">)</span> -o webbench webbench.o <span class="token variable">$</span><span class="token punctuation">(</span>LIBS<span class="token punctuation">)</span> 

<span class="token symbol">clean</span><span class="token punctuation">:</span>
	-rm -f *.o webbench *~ core *.core tags
	
<span class="token symbol">tar</span><span class="token punctuation">:</span>   clean
	-debian/rules clean
	rm -rf <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>
	install -d <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>
	cp -p Makefile webbench.c socket.c webbench.1 <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>
	install -d <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>/debian
	-cp -p debian/* <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>/debian
	ln -sf debian/copyright <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>/COPYRIGHT
	ln -sf debian/changelog <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span>/ChangeLog
	-cd <span class="token variable">$</span><span class="token punctuation">(</span>TMPDIR<span class="token punctuation">)</span> &amp;&amp; cd .. &amp;&amp; tar cozf webbench-<span class="token variable">$</span><span class="token punctuation">(</span>VERSION<span class="token punctuation">)</span>.tar.gz webbench-<span class="token variable">$</span><span class="token punctuation">(</span>VERSION<span class="token punctuation">)</span>

<span class="token symbol">webbench.o</span><span class="token punctuation">:</span>	webbench.c socket.c Makefile

<span class="token builtin">.PHONY</span><span class="token punctuation">:</span> clean install all tar

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><h2 id="_05-源码注释版"><a href="#_05-源码注释版" class="header-anchor">#</a> 05.源码注释版</h2> <ul><li>我将源码的风格进行排版，展示如下</li></ul> <h3 id="_5-1-socket-c"><a href="#_5-1-socket-c" class="header-anchor">#</a> 5.1.<code>socket.c</code></h3> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $
 *
 * This module has been modified by Radim Kolar for OS/2 emx
 */</span>

<span class="token comment">/***********************************************************************
  module:       socket.c
  program:      popclient
  SCCS ID:      @(#)socket.c    1.5  4/1/94
  programmer:   Virginia Tech Computing Center
  compiler:     DEC RISC C compiler (Ultrix 4.1)
  environment:  DEC Ultrix 4.3 
  description:  UNIX sockets code.
 ***********************************************************************/</span>
 
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/socket.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;netinet/in.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;arpa/inet.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;netdb.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/time.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdarg.h&gt;</span></span>

<span class="token comment">/************************
  功能：通过网络地址host和端口clientPort建立网络连接获得『TCP的套接字』
  参数：
    host：网络地址
    	注意：（host支持域名）
    clientPort:端口
  返回值：
  	建立的socket连接
  	如果返回 -1,表示建立连接失联
************************/</span>


<span class="token keyword">int</span> <span class="token function">Socket</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>host<span class="token punctuation">,</span> <span class="token keyword">int</span> clientPort<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> sock<span class="token punctuation">;</span><span class="token comment">//承载套接字『sock』</span>
    <span class="token keyword">unsigned</span> <span class="token keyword">long</span> inaddr<span class="token punctuation">;</span><span class="token comment">//承载</span>
    <span class="token keyword">struct</span> <span class="token class-name">sockaddr_in</span> ad<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">hostent</span> <span class="token operator">*</span>hp<span class="token punctuation">;</span><span class="token comment">//承载IP地址</span>
    
    <span class="token function">memset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ad<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>ad<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    ad<span class="token punctuation">.</span>sin_family <span class="token operator">=</span> AF_INET<span class="token punctuation">;</span>

    inaddr <span class="token operator">=</span> <span class="token function">inet_addr</span><span class="token punctuation">(</span>host<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//假设host是『点分制的IP地址』将点分的十进制的IP转为无符号长整型</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>inaddr <span class="token operator">!=</span> INADDR_NONE<span class="token punctuation">)</span>
        <span class="token function">memcpy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ad<span class="token punctuation">.</span>sin_addr<span class="token punctuation">,</span> <span class="token operator">&amp;</span>inaddr<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>inaddr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        hp <span class="token operator">=</span> <span class="token function">gethostbyname</span><span class="token punctuation">(</span>host<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//如果host是域名,用域名获取IP地址</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>hp <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token function">memcpy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ad<span class="token punctuation">.</span>sin_addr<span class="token punctuation">,</span> hp<span class="token operator">-&gt;</span>h_addr<span class="token punctuation">,</span> hp<span class="token operator">-&gt;</span>h_length<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//获取端口，将一个无符号短整型(s)的主机数值(h)转换为网络字节顺序(n)</span>
    ad<span class="token punctuation">.</span>sin_port <span class="token operator">=</span> <span class="token function">htons</span><span class="token punctuation">(</span>clientPort<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//创建通信『套接字』</span>
    sock <span class="token operator">=</span> <span class="token function">socket</span><span class="token punctuation">(</span>AF_INET<span class="token punctuation">,</span> SOCK_STREAM<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>sock <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">//连接到相应的主机</span>
        <span class="token keyword">return</span> sock<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">connect</span><span class="token punctuation">(</span>sock<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">sockaddr</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>ad<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>ad<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//连接不到</span>
    <span class="token keyword">return</span> sock<span class="token punctuation">;</span><span class="token comment">//连接到相应的主机</span>
<span class="token punctuation">}</span>


</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br></div></div><h3 id="_5-2-webbench-c主体函数剖析"><a href="#_5-2-webbench-c主体函数剖析" class="header-anchor">#</a> 5.2.<code>webbench.c</code>主体函数剖析</h3> <ul><li>webbench.c文件中，包含了下面几个函数：</li></ul> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//信号『处理』函数，时钟结束时进行调用</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">alarm_handler</span><span class="token punctuation">(</span><span class="token keyword">int</span> signal<span class="token punctuation">)</span>
    
<span class="token comment">//是在使用出错时提示怎么『使用』本程序</span>
<span class="token comment">//输出错误时候的『帮助信息』</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">usage</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>

<span class="token comment">//是用来创建『http连接』请求的</span>
<span class="token keyword">void</span> <span class="token function">build_request</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>url<span class="token punctuation">)</span>

<span class="token comment">//创建管道和子进程，调用测试http函数</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">bench</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>

<span class="token comment">//对http请求进行测试『核心代码』</span>
<span class="token keyword">void</span> <span class="token function">benchcore</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>host<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">int</span> port<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>req<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_5-3-webbench-c"><a href="#_5-3-webbench-c" class="header-anchor">#</a> 5.3.<code>webbench.c</code></h3> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
 * (C) Radim Kolar 1997-2004
 * This is free software, see GNU Public License version 2 for
 * details.
 *
 * Simple forking WWW Server benchmark:
 *
 * Usage:
 *   webbench --help
 *
 * Return codes:
 *    0 - sucess
 *    1 - benchmark failed (server is not on-line)
 *    2 - bad param
 *    3 - internal error, fork failed
 * 
 */</span> 
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&quot;socket.c&quot;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/param.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;rpc/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;getopt.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;strings.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;time.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>


<span class="token comment">/* values */</span>
<span class="token keyword">volatile</span> <span class="token keyword">int</span> timerexpired<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> speed<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>	<span class="token comment">//子进程成功得到服务器响应的总数</span>
<span class="token keyword">int</span> failed<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>	<span class="token comment">//子进程请求失败总数</span>
<span class="token keyword">int</span> bytes<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>	<span class="token comment">//读取的字节总数</span>
<span class="token comment">/* globals */</span>
<span class="token keyword">int</span> http10<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">/* 0 - http/0.9, 1 - http/1.0, 2 - http/1.1 */</span>
<span class="token comment">/* Allow: GET, HEAD, OPTIONS, TRACE */</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">METHOD_GET</span> <span class="token expression"><span class="token number">0</span>	</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">METHOD_HEAD</span> <span class="token expression"><span class="token number">1</span>	</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">METHOD_OPTIONS</span> <span class="token expression"><span class="token number">2</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">METHOD_TRACE</span> <span class="token expression"><span class="token number">3</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">PROGRAM_VERSION</span> <span class="token string">&quot;1.5&quot;</span></span>
<span class="token keyword">int</span> method<span class="token operator">=</span>METHOD_GET<span class="token punctuation">;</span>	<span class="token comment">//HTTP请求方法，默认GET 方式</span>
<span class="token keyword">int</span> clients<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>			<span class="token comment">//只模拟一个客户端,并发数</span>
<span class="token keyword">int</span> force<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>			<span class="token comment">//是否等待服务器应答。默认为不等待</span>
<span class="token keyword">int</span> force_reload<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>		<span class="token comment">//失败时</span>
<span class="token keyword">int</span> proxyport<span class="token operator">=</span><span class="token number">80</span><span class="token punctuation">;</span>		<span class="token comment">//代理服务器应答，访问端口为80</span>
<span class="token keyword">char</span> <span class="token operator">*</span>proxyhost<span class="token operator">=</span><span class="token constant">NULL</span><span class="token punctuation">;</span>	<span class="token comment">//代理服务器的地址</span>
<span class="token keyword">int</span> benchtime<span class="token operator">=</span><span class="token number">30</span><span class="token punctuation">;</span>		<span class="token comment">//模拟请求时间</span>
<span class="token comment">/* internal */</span>
<span class="token keyword">int</span> mypipe<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>			<span class="token comment">//管道，用于父子进程间通信</span>
<span class="token keyword">char</span> host<span class="token punctuation">[</span>MAXHOSTNAMELEN<span class="token punctuation">]</span><span class="token punctuation">;</span>	<span class="token comment">//网络地址</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">REQUEST_SIZE</span> <span class="token expression"><span class="token number">2048</span></span></span>
<span class="token keyword">char</span> request<span class="token punctuation">[</span>REQUEST_SIZE<span class="token punctuation">]</span><span class="token punctuation">;</span>	<span class="token comment">//HTTP请求信息</span>



<span class="token comment">//static修饰这个结构体，是为了让他不被其他头文件访问到</span>
<span class="token comment">//struct option这样的结构体方式，是按照纯C语言的来写的</span>
<span class="token keyword">static</span> <span class="token keyword">const</span> <span class="token keyword">struct</span> <span class="token class-name">option</span> long_options<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">=</span>
<span class="token punctuation">{</span>
    <span class="token punctuation">{</span><span class="token string">&quot;force&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token operator">&amp;</span>force<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;reload&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token operator">&amp;</span>force_reload<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;time&quot;</span><span class="token punctuation">,</span>required_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'t'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;help&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'?'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;http09&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'9'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;http10&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'1'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;http11&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'2'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;get&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token operator">&amp;</span>method<span class="token punctuation">,</span>METHOD_GET<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;head&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token operator">&amp;</span>method<span class="token punctuation">,</span>METHOD_HEAD<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;options&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token operator">&amp;</span>method<span class="token punctuation">,</span>METHOD_OPTIONS<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;trace&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token operator">&amp;</span>method<span class="token punctuation">,</span>METHOD_TRACE<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;version&quot;</span><span class="token punctuation">,</span>no_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'V'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;proxy&quot;</span><span class="token punctuation">,</span>required_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'p'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token string">&quot;clients&quot;</span><span class="token punctuation">,</span>required_argument<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>



<span class="token comment">/* prototypes */</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">benchcore</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span> host<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">int</span> port<span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">bench</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">build_request</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">alarm_handler</span><span class="token punctuation">(</span><span class="token keyword">int</span> signal<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
   timerexpired<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>	

<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">usage</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
   <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span>
	<span class="token string">&quot;webbench [option]... URL\n&quot;</span>
	<span class="token string">&quot;  -f|--force               Don't wait for reply from server.\n&quot;</span>
	<span class="token string">&quot;  -r|--reload              Send reload request - Pragma: no-cache.\n&quot;</span>
	<span class="token string">&quot;  -t|--time &lt;sec&gt;          Run benchmark for &lt;sec&gt; seconds. Default 30.\n&quot;</span>
	<span class="token string">&quot;  -p|--proxy &lt;server:port&gt; Use proxy server for request.\n&quot;</span>
	<span class="token string">&quot;  -c|--clients &lt;n&gt;         Run &lt;n&gt; HTTP clients at once. Default one.\n&quot;</span>
	<span class="token string">&quot;  -9|--http09              Use HTTP/0.9 style requests.\n&quot;</span>
	<span class="token string">&quot;  -1|--http10              Use HTTP/1.0 protocol.\n&quot;</span>
	<span class="token string">&quot;  -2|--http11              Use HTTP/1.1 protocol.\n&quot;</span>
	<span class="token string">&quot;  --get                    Use GET request method.\n&quot;</span>
	<span class="token string">&quot;  --head                   Use HEAD request method.\n&quot;</span>
	<span class="token string">&quot;  --options                Use OPTIONS request method.\n&quot;</span>
	<span class="token string">&quot;  --trace                  Use TRACE request method.\n&quot;</span>
	<span class="token string">&quot;  -?|-h|--help             This information.\n&quot;</span>
	<span class="token string">&quot;  -V|--version             Display program version.\n&quot;</span>
	<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>





<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> opt<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> options_index<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>tmp<span class="token operator">=</span><span class="token constant">NULL</span><span class="token punctuation">;</span>

    <span class="token comment">//不带参数时直接输出帮助信息</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>argc<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">usage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> 

    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>opt<span class="token operator">=</span><span class="token function">getopt_long</span><span class="token punctuation">(</span>argc<span class="token punctuation">,</span>argv<span class="token punctuation">,</span><span class="token string">&quot;912Vfrt:p:c:?h&quot;</span><span class="token punctuation">,</span>long_options<span class="token punctuation">,</span><span class="token operator">&amp;</span>options_index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token constant">EOF</span> <span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">switch</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">case</span>  <span class="token number">0</span> <span class="token operator">:</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'f'</span><span class="token operator">:</span> force<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'r'</span><span class="token operator">:</span> force_reload<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span> 
            <span class="token keyword">case</span> <span class="token string">'9'</span><span class="token operator">:</span> http10<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'1'</span><span class="token operator">:</span> http10<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'2'</span><span class="token operator">:</span> http10<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'V'</span><span class="token operator">:</span> <span class="token function">printf</span><span class="token punctuation">(</span>PROGRAM_VERSION<span class="token string">&quot;\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'t'</span><span class="token operator">:</span> benchtime<span class="token operator">=</span><span class="token function">atoi</span><span class="token punctuation">(</span>optarg<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>	     
            <span class="token keyword">case</span> <span class="token string">'p'</span><span class="token operator">:</span> 
                <span class="token comment">/* proxy server parsing server:port */</span>
                tmp<span class="token operator">=</span><span class="token function">strrchr</span><span class="token punctuation">(</span>optarg<span class="token punctuation">,</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                proxyhost<span class="token operator">=</span>optarg<span class="token punctuation">;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>tmp<span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token keyword">break</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>tmp<span class="token operator">==</span>optarg<span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;Error in option --proxy %s: Missing hostname.\n&quot;</span><span class="token punctuation">,</span>optarg<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>tmp<span class="token operator">==</span>optarg<span class="token operator">+</span><span class="token function">strlen</span><span class="token punctuation">(</span>optarg<span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;Error in option --proxy %s Port number is missing.\n&quot;</span><span class="token punctuation">,</span>optarg<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token operator">*</span>tmp<span class="token operator">=</span><span class="token string">'\0'</span><span class="token punctuation">;</span>
                proxyport<span class="token operator">=</span><span class="token function">atoi</span><span class="token punctuation">(</span>tmp<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">':'</span><span class="token operator">:</span>
            <span class="token keyword">case</span> <span class="token string">'h'</span><span class="token operator">:</span>
            <span class="token keyword">case</span> <span class="token string">'?'</span><span class="token operator">:</span> <span class="token function">usage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token string">'c'</span><span class="token operator">:</span> clients<span class="token operator">=</span><span class="token function">atoi</span><span class="token punctuation">(</span>optarg<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token comment">//#end switch</span>
        
    <span class="token punctuation">}</span><span class="token comment">//#end while</span>
 
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>optind<span class="token operator">==</span>argc<span class="token punctuation">)</span> 
    <span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;webbench: Missing URL!\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">usage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>clients<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> clients<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>benchtime<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> benchtime<span class="token operator">=</span><span class="token number">60</span><span class="token punctuation">;</span>
    
    <span class="token comment">/* Copyright */</span>
    <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;Webbench - Simple Web Benchmark &quot;</span>PROGRAM_VERSION<span class="token string">&quot;\n&quot;</span>
            <span class="token string">&quot;Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.\n&quot;</span>
           <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">build_request</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span>optind<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">/* print bench info */</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;\nBenchmarking: &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>method<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">case</span> METHOD_GET<span class="token operator">:</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> METHOD_OPTIONS<span class="token operator">:</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;OPTIONS&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> METHOD_HEAD<span class="token operator">:</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;HEAD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> METHOD_TRACE<span class="token operator">:</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;TRACE&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
 	
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot; %s&quot;</span><span class="token punctuation">,</span>argv<span class="token punctuation">[</span>optind<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>http10<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot; (using HTTP/0.9)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot; (using HTTP/1.1)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
 	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>clients<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span> 
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;1 client&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d clients&quot;</span><span class="token punctuation">,</span>clients<span class="token punctuation">)</span><span class="token punctuation">;</span>

    
 	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;, running %d sec&quot;</span><span class="token punctuation">,</span> benchtime<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>force<span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;, early socket close&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>proxyhost<span class="token operator">!=</span><span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;, via proxy server %s:%d&quot;</span><span class="token punctuation">,</span>proxyhost<span class="token punctuation">,</span>proxyport<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>force_reload<span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;, forcing reload&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;.\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">bench</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br><span class="line-number">112</span><br><span class="line-number">113</span><br><span class="line-number">114</span><br><span class="line-number">115</span><br><span class="line-number">116</span><br><span class="line-number">117</span><br><span class="line-number">118</span><br><span class="line-number">119</span><br><span class="line-number">120</span><br><span class="line-number">121</span><br><span class="line-number">122</span><br><span class="line-number">123</span><br><span class="line-number">124</span><br><span class="line-number">125</span><br><span class="line-number">126</span><br><span class="line-number">127</span><br><span class="line-number">128</span><br><span class="line-number">129</span><br><span class="line-number">130</span><br><span class="line-number">131</span><br><span class="line-number">132</span><br><span class="line-number">133</span><br><span class="line-number">134</span><br><span class="line-number">135</span><br><span class="line-number">136</span><br><span class="line-number">137</span><br><span class="line-number">138</span><br><span class="line-number">139</span><br><span class="line-number">140</span><br><span class="line-number">141</span><br><span class="line-number">142</span><br><span class="line-number">143</span><br><span class="line-number">144</span><br><span class="line-number">145</span><br><span class="line-number">146</span><br><span class="line-number">147</span><br><span class="line-number">148</span><br><span class="line-number">149</span><br><span class="line-number">150</span><br><span class="line-number">151</span><br><span class="line-number">152</span><br><span class="line-number">153</span><br><span class="line-number">154</span><br><span class="line-number">155</span><br><span class="line-number">156</span><br><span class="line-number">157</span><br><span class="line-number">158</span><br><span class="line-number">159</span><br><span class="line-number">160</span><br><span class="line-number">161</span><br><span class="line-number">162</span><br><span class="line-number">163</span><br><span class="line-number">164</span><br><span class="line-number">165</span><br><span class="line-number">166</span><br><span class="line-number">167</span><br><span class="line-number">168</span><br><span class="line-number">169</span><br><span class="line-number">170</span><br><span class="line-number">171</span><br><span class="line-number">172</span><br><span class="line-number">173</span><br><span class="line-number">174</span><br><span class="line-number">175</span><br><span class="line-number">176</span><br><span class="line-number">177</span><br><span class="line-number">178</span><br><span class="line-number">179</span><br><span class="line-number">180</span><br><span class="line-number">181</span><br><span class="line-number">182</span><br><span class="line-number">183</span><br><span class="line-number">184</span><br><span class="line-number">185</span><br><span class="line-number">186</span><br><span class="line-number">187</span><br><span class="line-number">188</span><br><span class="line-number">189</span><br><span class="line-number">190</span><br><span class="line-number">191</span><br><span class="line-number">192</span><br><span class="line-number">193</span><br><span class="line-number">194</span><br><span class="line-number">195</span><br><span class="line-number">196</span><br><span class="line-number">197</span><br><span class="line-number">198</span><br><span class="line-number">199</span><br><span class="line-number">200</span><br><span class="line-number">201</span><br><span class="line-number">202</span><br><span class="line-number">203</span><br><span class="line-number">204</span><br><span class="line-number">205</span><br><span class="line-number">206</span><br><span class="line-number">207</span><br><span class="line-number">208</span><br><span class="line-number">209</span><br><span class="line-number">210</span><br><span class="line-number">211</span><br><span class="line-number">212</span><br><span class="line-number">213</span><br><span class="line-number">214</span><br><span class="line-number">215</span><br><span class="line-number">216</span><br><span class="line-number">217</span><br><span class="line-number">218</span><br><span class="line-number">219</span><br><span class="line-number">220</span><br></div></div><h3 id="函数build-request"><a href="#函数build-request" class="header-anchor">#</a> 函数build_request</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">build_request</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>url<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">char</span> tmp<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> i<span class="token punctuation">;</span>

    <span class="token function">bzero</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span>MAXHOSTNAMELEN<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">bzero</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>REQUEST_SIZE<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>force_reload <span class="token operator">&amp;&amp;</span> proxyhost<span class="token operator">!=</span><span class="token constant">NULL</span> <span class="token operator">&amp;&amp;</span> http10<span class="token operator">&lt;</span><span class="token number">1</span><span class="token punctuation">)</span> http10<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>method<span class="token operator">==</span>METHOD_HEAD <span class="token operator">&amp;&amp;</span> http10<span class="token operator">&lt;</span><span class="token number">1</span><span class="token punctuation">)</span> http10<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>method<span class="token operator">==</span>METHOD_OPTIONS <span class="token operator">&amp;&amp;</span> http10<span class="token operator">&lt;</span><span class="token number">2</span><span class="token punctuation">)</span> http10<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>method<span class="token operator">==</span>METHOD_TRACE <span class="token operator">&amp;&amp;</span> http10<span class="token operator">&lt;</span><span class="token number">2</span><span class="token punctuation">)</span> http10<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>

    <span class="token keyword">switch</span><span class="token punctuation">(</span>method<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token keyword">case</span> METHOD_GET<span class="token operator">:</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> METHOD_HEAD<span class="token operator">:</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;HEAD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> METHOD_OPTIONS<span class="token operator">:</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;OPTIONS&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> METHOD_TRACE<span class="token operator">:</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;TRACE&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
		  
    <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token operator">==</span><span class="token function">strstr</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span><span class="token string">&quot;://&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">&quot;\n%s: is not a valid URL.\n&quot;</span><span class="token punctuation">,</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">strlen</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token operator">&gt;</span><span class="token number">1500</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;URL is too long.\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    
    
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>proxyhost<span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token operator">!=</span><span class="token function">strncasecmp</span><span class="token punctuation">(</span><span class="token string">&quot;http://&quot;</span><span class="token punctuation">,</span>url<span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 
        <span class="token punctuation">{</span> <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;\nOnly HTTP protocol is directly supported, set --proxy for others.\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    
    
    
    
    
    <span class="token comment">/* protocol/host delimiter */</span>
    i<span class="token operator">=</span><span class="token function">strstr</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span><span class="token string">&quot;://&quot;</span><span class="token punctuation">)</span><span class="token operator">-</span>url<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token comment">/* printf(&quot;%d\n&quot;,i); */</span>

    
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">strchr</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span> 
    <span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;\nInvalid URL syntax - hostname don't ends with '/'.\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>proxyhost<span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment">/* get port from hostname */</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">index</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token constant">NULL</span> <span class="token operator">&amp;&amp;</span>
           <span class="token function">index</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token function">index</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">strncpy</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token function">strchr</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token operator">-</span>url<span class="token operator">-</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">bzero</span><span class="token punctuation">(</span>tmp<span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">strncpy</span><span class="token punctuation">(</span>tmp<span class="token punctuation">,</span><span class="token function">index</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token function">strchr</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">index</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">/* printf(&quot;tmp=%s\n&quot;,tmp); */</span>
            proxyport<span class="token operator">=</span><span class="token function">atoi</span><span class="token punctuation">(</span>tmp<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>proxyport<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> proxyport<span class="token operator">=</span><span class="token number">80</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> 
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token function">strncpy</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token function">strcspn</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// printf(&quot;Host=%s\n&quot;,host);</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token operator">+</span><span class="token function">strlen</span><span class="token punctuation">(</span>request<span class="token punctuation">)</span><span class="token punctuation">,</span>url<span class="token operator">+</span>i<span class="token operator">+</span><span class="token function">strcspn</span><span class="token punctuation">(</span>url<span class="token operator">+</span>i<span class="token punctuation">,</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        
    <span class="token punctuation">}</span> 
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token comment">// printf(&quot;ProxyHost=%s\nProxyPort=%d\n&quot;,proxyhost,proxyport);</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>http10<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot; HTTP/1.0&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>http10<span class="token operator">==</span><span class="token number">2</span><span class="token punctuation">)</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot; HTTP/1.1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>http10<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;User-Agent: WebBench &quot;</span>PROGRAM_VERSION<span class="token string">&quot;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>proxyhost<span class="token operator">==</span><span class="token constant">NULL</span> <span class="token operator">&amp;&amp;</span> http10<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;Host: &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>host<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>force_reload <span class="token operator">&amp;&amp;</span> proxyhost<span class="token operator">!=</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;Pragma: no-cache\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    
    <span class="token keyword">if</span><span class="token punctuation">(</span>http10<span class="token operator">&gt;</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;Connection: close\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/* add empty line at end */</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>http10<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">strcat</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span><span class="token string">&quot;\r\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
    <span class="token comment">// printf(&quot;Req=%s\n&quot;,request);</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br><span class="line-number">112</span><br><span class="line-number">113</span><br><span class="line-number">114</span><br><span class="line-number">115</span><br><span class="line-number">116</span><br><span class="line-number">117</span><br><span class="line-number">118</span><br><span class="line-number">119</span><br><span class="line-number">120</span><br></div></div><h3 id="函数bench"><a href="#函数bench" class="header-anchor">#</a> 函数bench</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">/* vraci system rc error kod */</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">bench</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> i<span class="token punctuation">,</span>j<span class="token punctuation">,</span>k<span class="token punctuation">;</span>	
    pid_t pid<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    FILE <span class="token operator">*</span>f<span class="token punctuation">;</span>

    <span class="token comment">/* check avaibility of target server */</span>
    i<span class="token operator">=</span><span class="token function">Socket</span><span class="token punctuation">(</span>proxyhost<span class="token operator">==</span><span class="token constant">NULL</span><span class="token operator">?</span>host<span class="token operator">:</span>proxyhost<span class="token punctuation">,</span>proxyport<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">)</span> 
    <span class="token punctuation">{</span> 
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;\nConnect to server failed. Aborting benchmark.\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">close</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/* create pipe */</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">pipe</span><span class="token punctuation">(</span>mypipe<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;pipe failed.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/* not needed, since we have alarm() in childrens */</span>
    <span class="token comment">/* wait 4 next system clock tick */</span>
    <span class="token comment">/*
      cas=time(NULL);
      while(time(NULL)==cas)
            sched_yield();
      */</span>

    <span class="token comment">/* fork childs */</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>clients<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        pid<span class="token operator">=</span><span class="token function">fork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>pid <span class="token operator">&lt;=</span> <span class="token punctuation">(</span>pid_t<span class="token punctuation">)</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">/* child process or error*/</span>
            <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* make childs faster */</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span> pid<span class="token operator">&lt;</span> <span class="token punctuation">(</span>pid_t<span class="token punctuation">)</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;problems forking worker no. %d\n&quot;</span><span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;fork failed.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>pid<span class="token operator">==</span> <span class="token punctuation">(</span>pid_t<span class="token punctuation">)</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment">/* I am a child */</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>proxyhost<span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
            <span class="token function">benchcore</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span>proxyport<span class="token punctuation">,</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">else</span>
            <span class="token function">benchcore</span><span class="token punctuation">(</span>proxyhost<span class="token punctuation">,</span>proxyport<span class="token punctuation">,</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">/* write results to pipe */</span>
        f<span class="token operator">=</span><span class="token function">fdopen</span><span class="token punctuation">(</span>mypipe<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">&quot;w&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>f<span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open pipe for writing failed.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">/* fprintf(stderr,&quot;Child - %d %d\n&quot;,speed,failed); */</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span><span class="token string">&quot;%d %d %d\n&quot;</span><span class="token punctuation">,</span>speed<span class="token punctuation">,</span>failed<span class="token punctuation">,</span>bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">fclose</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> 
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        f<span class="token operator">=</span><span class="token function">fdopen</span><span class="token punctuation">(</span>mypipe<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">&quot;r&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>f<span class="token operator">==</span><span class="token constant">NULL</span><span class="token punctuation">)</span> 
        <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open pipe for reading failed.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token function">setvbuf</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span>_IONBF<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        speed<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
        failed<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
        bytes<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>

        <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            pid<span class="token operator">=</span><span class="token function">fscanf</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span><span class="token string">&quot;%d %d %d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>i<span class="token punctuation">,</span><span class="token operator">&amp;</span>j<span class="token punctuation">,</span><span class="token operator">&amp;</span>k<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>pid<span class="token operator">&lt;</span><span class="token number">2</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span><span class="token string">&quot;Some of our childrens died.\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            speed<span class="token operator">+=</span>i<span class="token punctuation">;</span>
            failed<span class="token operator">+=</span>j<span class="token punctuation">;</span>
            bytes<span class="token operator">+=</span>k<span class="token punctuation">;</span>
            <span class="token comment">/* fprintf(stderr,&quot;*Knock* %d %d read=%d\n&quot;,speed,failed,pid); */</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">--</span>clients<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        
	  <span class="token function">fclose</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span>

        
        
  		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;\nSpeed=%d pages/min, %d bytes/sec.\nRequests: %d susceed, %d failed.\n&quot;</span><span class="token punctuation">,</span>
          <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">(</span>speed<span class="token operator">+</span>failed<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>benchtime<span class="token operator">/</span><span class="token number">60.0f</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
		  <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>bytes<span class="token operator">/</span><span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span>benchtime<span class="token punctuation">)</span><span class="token punctuation">,</span>
		  speed<span class="token punctuation">,</span>
		  failed<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
    <span class="token keyword">return</span> i<span class="token punctuation">;</span>
    
<span class="token punctuation">}</span><span class="token comment">// end method bench</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br></div></div><h3 id="函数benchcore"><a href="#函数benchcore" class="header-anchor">#</a> 函数benchcore</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">benchcore</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>host<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">int</span> port<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>req<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> rlen<span class="token punctuation">;</span>
    <span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">1500</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> s<span class="token punctuation">,</span>i<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">sigaction</span> sa<span class="token punctuation">;</span>

    <span class="token comment">/* setup alarm signal handler */</span>
    sa<span class="token punctuation">.</span>sa_handler<span class="token operator">=</span>alarm_handler<span class="token punctuation">;</span>
    sa<span class="token punctuation">.</span>sa_flags<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">sigaction</span><span class="token punctuation">(</span>SIGALRM<span class="token punctuation">,</span><span class="token operator">&amp;</span>sa<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">alarm</span><span class="token punctuation">(</span>benchtime<span class="token punctuation">)</span><span class="token punctuation">;</span>

    rlen<span class="token operator">=</span><span class="token function">strlen</span><span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">;</span>
 nexttry<span class="token operator">:</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
 <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>timerexpired<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
       <span class="token keyword">if</span><span class="token punctuation">(</span>failed<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span>
       <span class="token punctuation">{</span>
          <span class="token comment">/* fprintf(stderr,&quot;Correcting failed by signal\n&quot;); */</span>
          failed<span class="token operator">--</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span>
       <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    s<span class="token operator">=</span><span class="token function">Socket</span><span class="token punctuation">(</span>host<span class="token punctuation">,</span>port<span class="token punctuation">)</span><span class="token punctuation">;</span>                          
    <span class="token keyword">if</span><span class="token punctuation">(</span>s<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> failed<span class="token operator">++</span><span class="token punctuation">;</span><span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token punctuation">}</span> 
    <span class="token keyword">if</span><span class="token punctuation">(</span>rlen<span class="token operator">!=</span><span class="token function">write</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>req<span class="token punctuation">,</span>rlen<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>failed<span class="token operator">++</span><span class="token punctuation">;</span><span class="token function">close</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>http10<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> 
	    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">shutdown</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> failed<span class="token operator">++</span><span class="token punctuation">;</span><span class="token function">close</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>force<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> 
    <span class="token punctuation">{</span>
            <span class="token comment">/* read all available data from socket */</span>
	    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	    <span class="token punctuation">{</span>
              <span class="token keyword">if</span><span class="token punctuation">(</span>timerexpired<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span> 
	      i<span class="token operator">=</span><span class="token function">read</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>buf<span class="token punctuation">,</span><span class="token number">1500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token comment">/* fprintf(stderr,&quot;%d\n&quot;,i); */</span>
	      <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">)</span> 
              <span class="token punctuation">{</span> 
                 failed<span class="token operator">++</span><span class="token punctuation">;</span>
                 <span class="token function">close</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                 <span class="token keyword">goto</span> nexttry<span class="token punctuation">;</span>
              <span class="token punctuation">}</span>
	       <span class="token keyword">else</span>
		       <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
		       <span class="token keyword">else</span>
			       bytes<span class="token operator">+=</span>i<span class="token punctuation">;</span>
	    <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
     <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">close</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>failed<span class="token operator">++</span><span class="token punctuation">;</span><span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
     speed<span class="token operator">++</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
    
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br></div></div><h2 id="_06-提取剖析知识"><a href="#_06-提取剖析知识" class="header-anchor">#</a> 06.提取剖析知识</h2> <h3 id="_6-1-代码规范"><a href="#_6-1-代码规范" class="header-anchor">#</a> 6.1.代码规范</h3> <ul><li>该项目的代码风格不是很好，如果大型项目是这种风格会让人抓狂，好在这个项目代码量挺少。</li> <li>需要引以为戒</li></ul> <h3 id="_6-2-编程技巧"><a href="#_6-2-编程技巧" class="header-anchor">#</a> 6.2.编程技巧</h3> <ul><li>『业务代码模块分离』</li></ul> <h3 id="_6-3-词汇整理"><a href="#_6-3-词汇整理" class="header-anchor">#</a> 6.3.词汇整理</h3> <div class="language-c line-numbers-mode"><pre class="language-c"><code>prototypes，n<span class="token punctuation">,</span>原型；典型；（prototype的复数）	<span class="token comment">//代码中指的是『函数原型』的意思</span>

handler，n，操作者；处理者；处理器；操作装置；销赃者的从犯<span class="token comment">//『代码中函数原型』处理</span>
usage，n，使用；惯例；正确用法；惯用法；常规做法	<span class="token comment">//『代码中函数原型』</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/20.b5af7fad.js" defer></script>
  </body>
</html>
